package org.java.mathgame.screens;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import org.java.mathgame.Grid;
import org.java.mathgame.GridButton;
import org.java.mathgame.MathGame;

import java.util.ArrayList;
import java.util.Random;



public class GameScreen implements Screen {
    private MathGame game;
    private SpriteBatch spriteBatch;
    private BitmapFont font;
    public static Grid table;
    private Stage stage;
    private Stage dialogStage;
    private int gridSize;
    private String output;
    private String equation;
    private int safeTimer;
    private float cellSize;
    private ArrayList<TextButton.TextButtonStyle> gridButtonStyles;
    private ArrayList<TextButton.TextButtonStyle> operatorButtonStyles;
    private Random random = new Random();
    private Table rootTable;
    private GridButton goalButton;
    private long gameStartedTime;
    private TextButton equationText;
    private boolean gameOver;
    public int score;
    public int gameTime;
    public TextButton.TextButtonStyle infoEquationStyle;
    private Label scoreNumber;
    private Label timerNumber;
    private boolean scoreSent;
    private ArrayList<Disposable> disposables;
    private TextButton.TextButtonStyle flashStyle;
    private Timer timer;
    private TextButton.TextButtonStyle operatorButtonStyle;
    private TextButton.TextButtonStyle gridButtonStyle;

    // constructor to keep a reference to the main Game class
    public GameScreen(final MathGame game) {
        timer = new Timer();
        disposables = new ArrayList<Disposable>();
        float gdxWidth = Gdx.graphics.getWidth();
        float gdxHeight = Gdx.graphics.getHeight();
        this.game = game;
        output = "";
        equation = "";
        gridSize = 5;
        cellSize = gdxWidth / gridSize;
        spriteBatch = new SpriteBatch();
        font = new BitmapFont();
        font.setOwnsTexture(true);
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        BitmapFont buttonFont = generator.generateFont((int) ((gdxWidth/4f)/2f));
        buttonFont.setOwnsTexture(true);
        disposables.add(buttonFont);

        //TextButton.TextButtonStyle gridButtonStyle = new TextButton.TextButtonStyle();
        //TextButton.TextButtonStyle operatorButtonStyle;
        Color[] colors = new Color[]{
                new Color(210 / 255f, 57 / 255f, 59 / 255f, 1),
                new Color(58 / 255f, 143 / 255f, 208 / 255f, 1),
                new Color(59 / 255f, 209 / 255f, 112 / 255f, 1),
                new Color(208 / 255f, 147 / 255f, 58 / 255f, 1)};
        Color[] darkColors = new Color[]{
                new Color(162 / 255f, 9 / 255f, 11 / 255f, 1),
                new Color(10 / 255f, 95 / 255f, 160 / 255f, 1),
                new Color(11 / 255f, 161 / 255f, 62 / 255f, 1),
                new Color(162 / 255f, 98 / 255f, 10 / 255f, 1)};
        Color[] lightColors = new Color[]{
                new Color(217 / 255f, 157 / 255f, 157 / 255f, 1),
                new Color(161 / 255f, 213 / 255f, 239 / 255f, 1),
                new Color(165 / 255f, 240 / 255f, 200 / 255f, 1),
                new Color(232 / 255f, 199 / 255f, 130 / 255f, 1)};
        gridButtonStyles = new ArrayList<TextButton.TextButtonStyle>();
        int styleNumber = random.nextInt(4);
        for (int i = 0; i < 4; i++) {
            TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
            style.font = buttonFont;
            style.pressedOffsetX = 1;
            style.pressedOffsetY = -1;
            style.font.setColor(Color.WHITE);
            style.up = new ButtonBackground(colors[i]);
            style.down = new ButtonBackground(darkColors[i]);
            style.checked = new ButtonBackground(lightColors[i]);
            gridButtonStyles.add(style);
        }
        operatorButtonStyles = new ArrayList<TextButton.TextButtonStyle>();
        for (int i = 0; i < 4; i++) {
            TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
            style.font = buttonFont;
            style.pressedOffsetX = 1;
            style.pressedOffsetY = -1;
            style.font.setColor(Color.WHITE);
            style.up = new ButtonBackground(darkColors[i]);
            style.down = new ButtonBackground(colors[i]);
            style.checked = new ButtonBackground(lightColors[i]);
            operatorButtonStyles.add(style);
        }

        operatorButtonStyle = operatorButtonStyles.get(styleNumber);
        gridButtonStyle = gridButtonStyles.get(styleNumber);
        flashStyle = new TextButton.TextButtonStyle(gridButtonStyle);
        flashStyle.up = new ButtonBackground(lightColors[styleNumber]);

        //grid = new Grid(this, gridSize);
        random = new Random();
        table = new Grid(this, gridSize, cellSize, gridButtonStyle, operatorButtonStyle);
        stage = new Stage(gdxWidth,gdxHeight,true,spriteBatch);
        dialogStage = new Stage(gdxWidth,gdxHeight,true,spriteBatch);
        System.out.println(table.getHeight());
        table.setBounds(0, table.getHeight(), gdxWidth, gdxWidth * (5f / 4));
        table.setWidth(gdxWidth);
        table.pad(0, 10, 10, 10);
        //table.debug();

        float topPanelHeight = gdxHeight - table.getHeight();
        Label.LabelStyle labelStyle = new Label.LabelStyle();  //TODO: make a new style
        BitmapFont labelFont = generator.generateFont((int) (topPanelHeight/9));
        labelFont.setOwnsTexture(true);
        disposables.add(labelFont);
        labelStyle.font = labelFont;
        labelStyle.fontColor = Color.BLACK;

        infoEquationStyle = new TextButton.TextButtonStyle();
        infoEquationStyle.up = new ButtonBackground(new Color (35/255f,35/255f,35/255f,1));
        infoEquationStyle.font = generator.generateFont((int) (topPanelHeight/7f));
        infoEquationStyle.font.setOwnsTexture(true);
        disposables.add(infoEquationStyle.font);
        Table infoTable = new Table();
        Table infoLeft = new Table();
        Table infoRight = new Table();
        //infoRight.setFillParent(true);
        //infoLeft.setFillParent(true);
        TextButton.TextButtonStyle infoBackStyle = new TextButton.TextButtonStyle(infoEquationStyle);
        TextButton backButton = new TextButton("Back",infoBackStyle);
        TextButton.TextButtonStyle backStyle = new TextButton.TextButtonStyle(infoEquationStyle);
        backStyle.font = generator.generateFont((int) (topPanelHeight/7f));
        backStyle.font.setOwnsTexture(true);
        disposables.add(backStyle.font);
        backStyle.up = new ButtonBackground(new Color(0,0,0,0));
        TextButton backArrow = new TextButton("<",backStyle);
        backButton.clear();
        backButton.add(backArrow).height(backButton.getHeight()).expandX().left().padLeft(5);
        backButton.add(backButton.getLabel()).padRight(5);
        backButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button){
                game.setScreen(game.mainMenuScreen);
            }
        });
        equationText = new TextButton("",infoEquationStyle);
        Label goalLabel = new Label("GOAL", labelStyle);
        Label scoreLabel = new Label("SCORE", labelStyle);
        Label timerLabel = new Label("TIME", labelStyle);
        Label equationLabel = new Label("EQUATION", labelStyle);

        Label.LabelStyle numberStyle = new Label.LabelStyle();
        numberStyle.font = generator.generateFont((int) (topPanelHeight/4f));
        numberStyle.fontColor = darkColors[styleNumber];

        scoreNumber = new Label("0", numberStyle);
        timerNumber = new Label("0", numberStyle);
        Texture refreshTexture = new Texture("data/refresh-icon.png");
        disposables.add(refreshTexture);
        refreshTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
        ImageButton refreshButton = new ImageButton(new SpriteDrawable(new Sprite(refreshTexture)));
        goalButton = new GridButton(0f, operatorButtonStyle, -1, -1, 0f);
        goalButton.setText(Float.toString(table.getResult()));
        goalButton.setBackground(new ButtonBackground(new Color(35 / 255f, 35 / 255f, 35 / 255f, 1)));
        goalButton.setDisabled(true);
        infoRight.add(goalLabel).expandX().left().expandY().fillY();
        infoRight.row();
        infoRight.add(goalButton).expandX().fillX().expandY().fillY();
        infoLeft.add(scoreLabel).expand(true, false).left().padLeft(10);
        infoLeft.add(timerLabel).expand(true, false).left();
        infoLeft.row();
        infoLeft.add(scoreNumber).padLeft(10).expandY().fillY().expandX().left();
        infoLeft.add(timerNumber).expandY().fillY().expandX().left();
        //refreshButton.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("data/refresh-icon.png")))));
        refreshButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                table.refresh();
            }
        });
        infoLeft.add(refreshButton).width(topPanelHeight/4f).height(topPanelHeight/4f);
        infoLeft.row();
        infoLeft.add(equationLabel).colspan(3).expandX().padLeft(10);
        infoLeft.row();
        infoLeft.add(equationText).colspan(3).expandX().height(topPanelHeight/7).fillX().padLeft(10);
        infoTable.setWidth(gdxWidth);
        infoTable.add(backButton).expandX().expandY().top().colspan(2).fillX();
        infoTable.row();
        infoTable.add(infoLeft).expandX().fillX().left().expandY();//.height(gdxHeight - table.getHeight());
        infoTable.add(infoRight).fillX().right().expandY().fillY().width(gdxWidth/3).pad(10);//.height(gdxHeight - table.getHeight());
        infoTable.setWidth(gdxWidth);
        infoTable.setBackground(new ButtonBackground(new Color(203 / 255f, 203 / 255f, 203 / 255f, 1)));

        //infoLeft.debug();
        //infoRight.debug();
        //infoTable.debug();
        rootTable = new Table();
        rootTable.setHeight(gdxHeight);
        rootTable.setWidth(gdxWidth);
        //rootTable.debug();
        rootTable.setFillParent(true);
        rootTable.add(infoTable).expandY().fillY().expandX().fillX();
        rootTable.row();
        rootTable.add(table).expandY().fillY().expandX().fillX();
        stage.addActor(rootTable);
        Gdx.input.setInputProcessor(stage);
        generator.dispose();
        game.setGameRunning(true);
        setGameOver(false);
        setGameStartedTime(System.currentTimeMillis());
        if (game.getGameMode() == 1)
            setGameTime(60);
        else
            setGameTime(0);
        scoreSent = false;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public boolean isGameOver() {
        return gameOver;
    }

    public void setGameOver(boolean gameOver) {
        this.gameOver = gameOver;
    }

    public int getGameTime() {
        return gameTime;
    }

    public void setGameTime(int gameTime) {
        this.gameTime = gameTime;
    }

    public long getGameStartedTime() {
        return gameStartedTime;
    }

    public void setGameStartedTime(long gameStartedTime) {
        this.gameStartedTime = gameStartedTime;
    }

    public void flashGrid(String type) {
        if (type.equalsIgnoreCase("operator")) {
            for(final GridButton button:table.getButtonList()){
                if(!button.isNumber()){
                    button.setStyle(flashStyle);
                    timer.schedule(new Task() {
                        @Override
                        public void run() {
                            button.setStyle(gridButtonStyle);
                            //timer.cancel();
                        }
                    }, 0.5f);
                }
            }
        } else if (type.equalsIgnoreCase("number")) {
            for(final GridButton button:table.getButtonList()){
                if(button.isNumber()){
                    button.setStyle(flashStyle);
                    timer.schedule(new Task() {
                        @Override
                        public void run() {
                            button.setStyle(operatorButtonStyle);
                            //timer.cancel();
                        }
                    }, 0.5f);
                }
            }
        }
        //for(TextButton button:table.getButtonList()){
        //     button.setStyle(flashStyle);
        //}
    }

    public MathGame getGame() {
        return this.game;
    }

    public void setOutput(String text, String type) {
        if (type.equalsIgnoreCase("clear"))
            output = text;
        else if (type.equalsIgnoreCase("add"))
            output += text;
    }

    public void setEquation(String text, String type) {
        if (type.equalsIgnoreCase("clear"))
            equation = text;
        else if (type.equalsIgnoreCase("add"))
            equation += text;
    }

    public void drawGameOver() {
        if (!scoreSent) {
            scoreSent = true;
            game.getHighScoreHandler().checkHighScore("roberts", getScore(), game.getGameMode());
        }
        for (GridButton button: table.getButtonList())
            button.setChecked(false);
        spriteBatch.begin();
        font.draw(spriteBatch, "GAME OVER YO!", Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
        font.draw(spriteBatch, "SCORE: " + score, Gdx.graphics.getWidth() / 2, (Gdx.graphics.getHeight() / 2) - font.getLineHeight());
        font.draw(spriteBatch, "press M for menu; N for new game" + score, Gdx.graphics.getWidth() / 2, (Gdx.graphics.getHeight() / 2) - font.getLineHeight()*2);
        spriteBatch.end();
        if (Gdx.input.isKeyPressed(Input.Keys.M)) {
            game.setScreen(game.mainMenuScreen);
        }
        if (Gdx.input.isKeyPressed(Input.Keys.N)) {
            game.setScreen(game.gameSettingsScreen);
        }
    }

    public void tick() {
        if (game.isGameRunning()) {
            long gameTimePassed = System.currentTimeMillis() - getGameStartedTime();
            if (gameTimePassed / 500 > 1) {
                if (game.getGameMode() == 1)
                    setGameTime(getGameTime() - 1);
                else if (game.getGameMode() == 2)
                    setGameTime(getGameTime() + 1);
                setGameStartedTime(System.currentTimeMillis());
            }
        }
        if (game.getGameMode() == 1 && getGameTime() == 0) {
            setGameOver(true);
            onGameOver();
            game.setGameRunning(false);
        }
    }

    public void onGameOver(){
        System.out.println("Game over yo!");
        if (game.getHighScoreHandler().isNewHighScore(score,game.getGameMode())){
            highScoreDialog();
        }
        else{
            gameOverDialog();
        }
    }
    public void highScoreDialog(){
        int gdxWidth = Gdx.graphics.getWidth();
        int gdxHeight = Gdx.graphics.getHeight();
        Table dialog = new Table();

        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        disposables.add(generator);
        dialog.setHeight(gdxHeight / 3f);
        dialog.setWidth(gdxWidth * (4 / 5f));
        dialog.setBackground(new ButtonBackground(new Color(238 / 255f, 238 / 255f, 238 / 255f, 1f)));
        dialog.setX((gdxWidth-dialog.getWidth())/2f);
        dialog.setY(gdxHeight * (2 / 3f));

        final TextField input = new TextField(" ", new TextField.TextFieldStyle(generator.generateFont((int) (dialog.getHeight() / 8f)), Color.BLACK, new ButtonBackground(new Color(0, 0, 0, 1)), new ButtonBackground(new Color(0.5f, 0.5f, 0.5f, 1)), new ButtonBackground(new Color(0.7f, 0.7f, 0.7f, 1))));
        input.getStyle().font.setOwnsTexture(true);
        disposables.add(input.getStyle().font);

        input.getStyle().font = generator.generateFont((int) (dialog.getHeight()/8f));
        input.getStyle().font.setOwnsTexture(true);
        disposables.add(input.getStyle().font);

        TextButton.TextButtonStyle infoStyle = new TextButton.TextButtonStyle();
        infoStyle.up = new ButtonBackground(new Color(35/255f,35/255f,35/255f,1));

        infoStyle.font = generator.generateFont((int) (dialog.getHeight()/8f));
        infoStyle.font.setOwnsTexture(true);
        disposables.add(infoStyle.font);
        Label.LabelStyle rowStyle = new Label.LabelStyle();
        rowStyle.font = generator.generateFont((int) (dialog.getHeight()/8));
        rowStyle.font.setOwnsTexture(true);
        disposables.add(rowStyle.font);
        rowStyle.fontColor = Color.BLACK;
        TextButton okButton = new TextButton("OK",gridButtonStyle);
        okButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button){
                dialogStage.clear();
                Gdx.input.setOnscreenKeyboardVisible(false);
                game.getHighScoreHandler().checkHighScore(input.getText(),score,game.getGameMode());
                gameOverDialog();

            }
        });
        TextButton titleLabel = new TextButton("New High Score!",infoStyle);
        Label inputLabel = new Label("Enter your name:",rowStyle);
        dialog.setColor(Color.BLACK);
        dialog.add(titleLabel).colspan(2).expandX().fillX().left();
        dialog.row();
        dialog.add(inputLabel).expandX().fillX().pad(0,5,0,5);
        dialog.row();
        dialog.add(input).expandX().fillX().pad(0, 5, 0, 5);
        dialog.row();
        dialog.add(okButton).expandY().bottom().right().pad(0,5,5,5);
        dialog.pad(5);
        dialogStage.clear();
        dialogStage.addActor(dialog);
        dialogStage.setKeyboardFocus(input);
        Gdx.input.setOnscreenKeyboardVisible(true);
        Gdx.input.setInputProcessor(dialogStage);
        //dialog.debug();
    }

    public void gameOverDialog(){
        int gdxWidth = Gdx.graphics.getWidth();
        int gdxHeight = Gdx.graphics.getHeight();
        Table dialog = new Table();

        dialog.setHeight(gdxHeight / 3f);
        dialog.setWidth(gdxWidth * (3 / 5f));
        dialog.setBackground(new ButtonBackground(new Color(203 / 255f, 203 / 255f, 203 / 255f, 1)));
        dialog.setX((gdxWidth-dialog.getWidth())/2f);
        dialog.setY(gdxHeight * (1 / 3f));


        TextButton.TextButtonStyle infoStyle = new TextButton.TextButtonStyle();
        infoStyle.up = new ButtonBackground(new Color(35/255f,35/255f,35/255f,1));
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        disposables.add(generator);
        infoStyle.font = generator.generateFont((int) (dialog.getHeight()/8f));
        infoStyle.font.setOwnsTexture(true);
        disposables.add(infoStyle.font);

        Label.LabelStyle rowStyle = new Label.LabelStyle();
        rowStyle.font = generator.generateFont((int) (dialog.getHeight()/8));
        rowStyle.font.setOwnsTexture(true);
        disposables.add(rowStyle.font);
        rowStyle.fontColor = Color.BLACK;
        TextButton okButton = new TextButton("OK",gridButtonStyle);
        okButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button){
                dialogStage.clear();
                game.setGameRunning(false);
                game.setScreen(game.mainMenuScreen);
            }
        });
        TextButton titleLabel = new TextButton("Game over",infoStyle);
        Label timeLabel = new Label("Time:",rowStyle);
        Label timeNumber = new Label(Integer.toString(gameTime),rowStyle);
        Label scoreLabel = new Label("Score:",rowStyle);
        Label scoreNumber = new Label(Integer.toString(score),rowStyle);
        Label bestLabel = new Label("Best:",rowStyle);
        Label bestNumber = new Label(Integer.toString(game.getHighScoreHandler().getHighScore(game.getGameMode())[1].getScore()),rowStyle);
        dialog.setColor(Color.BLACK);
        dialog.add(titleLabel).colspan(2).fillX().left();
        dialog.row();
        if(game.getGameMode() == 2){
            dialog.add(timeLabel).left().pad(0,5,0,5);
            dialog.add(timeNumber).expandX().left();
            dialog.row();
        }
        dialog.add(scoreLabel).left().pad(0,5,0,5);
        dialog.add(scoreNumber).expandX().left();
        dialog.row();
        dialog.add(bestLabel).left().pad(0, 5, 0, 5);
        dialog.add(bestNumber).expandX().left();
        dialog.row();
        dialog.add(okButton).colspan(2).expandX().center().expandY().bottom().right();
        dialog.pad(5);
        //dialog.debug();
        dialogStage.clear();
        dialogStage.addActor(dialog);
        Gdx.input.setInputProcessor(dialogStage);
    }

    @Override
    public void render(float delta) {
        if (!gameOver) tick();
        goalButton.setText(String.format("%.0f",(Float) table.getResult()));
        equationText.setText(equation);
        scoreNumber.setText(String.valueOf(getScore()));
        timerNumber.setText(String.valueOf(getGameTime()));
        Gdx.gl.glClearColor(238 / 255f, 238 / 255f, 238 / 255f, 1f);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        //font.setColor(1f, 1f, 1f, 1f);
        stage.draw();
        dialogStage.draw();
        //Table.drawDebug(stage);
        spriteBatch.begin();
        //font.draw(spriteBatch, "gameScreen", Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight());
        //font.draw(spriteBatch, String.valueOf(getGameTime()), Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() - font.getLineHeight());
        //font.draw(spriteBatch, equation, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() - (font.getLineHeight() * 2));
        //font.draw(spriteBatch, "need to get: " + grid.getResult() + "", Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() - (font.getLineHeight()*3));
        spriteBatch.end();
        //if (isGameOver()) {
        //    drawGameOver();
        //}
    }


    @Override
    public void resize(int width, int height) {

    }


    @Override
    public void show() {
        // called when this screen is set as the screen with game.setScreen()
        Gdx.input.setInputProcessor(stage);
        safeTimer = 0;
        while (safeTimer < 5)
            safeTimer++;
        table.clear();
        // because InputListener touchUp method is called immediately otherwise
        setGameStartedTime(System.currentTimeMillis());
    }


    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
        // called when current screen changes from this to a different screen
    }


    @Override
    public void pause() {
    }


    @Override
    public void resume() {
    }


    @Override
    public void dispose() {
        // never called automatically


        spriteBatch.dispose();
        font.dispose();
        stage.dispose();



        for(Disposable disp: disposables){
            disp.dispose();
        }
    }
}

class ButtonBackground implements Drawable {
    private Color color;
    //private TextureRegion tRegion;
    private Pixmap pixmap;
    private float LWidth, RWidth, THeight, BHeight, MinWidth, MinHeight;

    public ButtonBackground(Color color) {
        this.color = color;

    }
    public void finalize(){
        pixmap.dispose();
    }

    public void draw(SpriteBatch batch, float x, float y, float width, float height) {
        pixmap = new Pixmap(64, 64, Pixmap.Format.RGBA8888);
        pixmap.setColor(color);
        pixmap.fill();
        Texture texture = new Texture(pixmap);
        TextureRegion tRegion = new TextureRegion(texture);
        batch.setColor(color);
        batch.draw(tRegion, x, y, width, height);
        texture.dispose();
        pixmap.dispose();
    }

    //Stuff required by the interface
    public float getLeftWidth() {
        return this.LWidth;
    }

    public void setLeftWidth(float leftWidth) {
        this.LWidth = leftWidth;
    }

    public float getRightWidth() {
        return RWidth;
    }

    public void setRightWidth(float rightWidth) {
        this.RWidth = rightWidth;
    }

    public float getTopHeight() {
        return THeight;
    }

    public void setTopHeight(float topHeight) {
        this.THeight = topHeight;
    }

    public float getBottomHeight() {
        return BHeight;
    }

    public void setBottomHeight(float bottomHeight) {
        this.BHeight = bottomHeight;
    }

    public float getMinWidth() {
        return MinWidth;
    }

    public void setMinWidth(float minWidth) {
        this.MinWidth = minWidth;
    }

    public float getMinHeight() {
        return MinHeight;
    }

    public void setMinHeight(float minHeight) {
        this.MinHeight = minHeight;
    }
}
